/************************公告管理js*********************************/
define(function(require) {
	
	var $ = require("jquery1.7");
	var internalEmailService = require("bean/internalEmailService");
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var ui = require("ui");
	
	
	$(document).ready(function(){
		var inx = $(".p_publisher").length;
		for(var i=0;i<inx;i++){
			var id = $(".p_publisher").eq(i).attr("data");
			var result = internalEmailService.findLinkManById(id);
			
			for(var j=0;j<result.data.length;j++){
				htm += result.data[j].nickname + ",";
			}
			$(".p_publisher").eq(i).html(htm);
		}
		

	});
	
	//初始表格
	var table = layui.table;
	var tablelin = table.render({
	    elem: '#demo'
	    ,url : '/back/website/internalEmailData'
	    ,cols: [[ //标题栏
	      {checkbox: true}
	      ,{field: 'title', title: '邮件标题', width: 200}
	      ,{field: 'addressee',title: '收件人',width:200}
	      ,{field: 'submiTime', title: '提交时间', width:180}
	      ,{field: 'pusTime', title: '发布时间', width:180}
	      ,{field: 'issueStatus', title: '发布状态', templet:'#iStatusTpl', width:100}
	      ,{field: '', title: '操作', width: 300,templet:'#barDemo'}
	      ]] 
	    ,even: true
	    ,page: true //是否显示分页
	    ,limits: [8, 12, 15,18]
	    ,limit: 8 //每页默认显示的数量
	    ,id: "test"
	  });
	
	
	var layer = layui.layer;
	//添加邮件
	$("#addEmailBtn").bind("click",function(){
		location.href = ctx + "/back/website/emailContent";
	});
	//更换layui实现，替换以前的blackBox被替换的原代码在最后注释中
	//批量删除
	$("#deleteEmailBtn").bind("click", function(){
		var checkStatus = layui.table.checkStatus('test');
		if (checkStatus.data.length===0) {
			layer.alert("请选择邮件");
			return;
		}
		layer.confirm("确定要删除邮件？",{icon: 3, title:'提示'},function(isOk){
			if(isOk){
				var data = checkStatus.data;
				var array = [];
				for(var i = 0; i<data.length;i++){
					array.push(data[i].id);   
				}
				if (array.length != 0) {
					try{
						ui.mask().show("请稍候...");
						internalEmailService.deleteEmail(array);
						ui.mask().hide();
						layer.alert("删除成功",function(){
							
							window.location.reload(true);
						});
					} catch (e) {
						ui.mask().hide();
						layer.alert("删除失败:"+e.message);
					}
				}
			}
			layer.close(isOk);
		});
	});
	//发送
	$("#issueEmailBtn").bind("click",function(){
		var checkStatus = layui.table.checkStatus('test');
		if (checkStatus.data.length!==1) {
			layer.alert("请选择一条邮件");
			return;
		}
		
		layer.confirm("确定要发送？",{icon: 3, title:'提示'},function(isOk){
			if(isOk){
				var data = checkStatus.data;
				var email_id=data[0].id;
				try{
					ui.mask().show("请稍候...");
					internalEmailService.issueNotice(email_id);
					ui.mask().hide();
					layer.alert("发送成功",function(){
						
						window.location.reload(true);
					});
				}catch (e) {
					ui.mask().hide();
					layer.alert("发送失败:"+e.message);
				}
			}
		});
	});
	
	//撤回邮件
	$(document).on("click",".abolishEmail",function(){	
		var id = $(this).attr("data").replace("email_", "");
		var issueStatus=internalEmailService.abolishEmail(id);
		if(issueStatus==0){
			layer.alert("未发送邮件不需撤回。");
			return;
		}
		layer.alert("撤回成功！！！",function(){
			location = location;
			
		});
	});
	//修改邮件
	$(document).on("click",".updateEmail",function(){	
		var id = $(this).attr("data").replace("email_", "");
		var issueStatus = $(this).parent().parent().parent().html();
		if(issueStatus=="已发送"){
			layer.alert("已发送的邮件不可修改");
			return;
		}
		location.href = ctx + "/back/website/emailContent?emailId="+id;
	});
	
	//查看邮件详细信息
	$(document).on("click",".checkEmailDetail",function(){	
		var id = $(this).attr("data");
		location.href = ctx + "/back/website/emailDetail?emailId="+id;
	});
	

	
	//查询邮件
	$(document).on("click","#searchBtn",function(){		
		var param = {
				title:$("#search_input").val()
		}
		tablelin.reload({
			where:param
		});
	})
	
	

});

